layui.use(['element', 'jquery', 'form', 'cmconfig', 'cmrouter', 'cmwindow', 'sysUser', 'cmhttp'], function() {
	var $ = layui.$,
		cmrouter = layui.cmrouter,
		cmwindow = layui.cmwindow,
		sysUser = layui.sysUser,
		cmhttp = layui.cmhttp,
		cmconfig = layui.cmconfig,
		form = layui.form;

	var elms = {
		submitBtn: $('button[lay-filter="save"]')
	};

	var datas = {
		
	};
	
	var methods = {
		_fmtData: function(str) {
			var jsencrypt = new JSEncrypt();
			jsencrypt.setPublicKey(cmconfig.winRSAPublicKey);
			// enData用来装载加密后的数据
			return jsencrypt.encrypt(str);
		},
		isEqual: function(newPwd, confirmPwd){
			if(newPwd === confirmPwd){
				return true;
			}
			
			return false;
		}
	};
	
	
	
	/**
	 * 事件处理
	 */
	
	form.on('submit(save)', function(data) {
	
		elms.submitBtn.addClass('layui-btn-disabled');
		
		var newPwd = data.field.newPWD,
			oldPwd = data.field.oldPWD,
			confirmPwd = data.field.confirmPWD;
		
		if(methods.isEqual(newPwd, confirmPwd)){
			
			data.field.newPWD = methods._fmtData(newPwd);
			data.field.oldPWD = methods._fmtData(oldPwd);
			
			sysUser.updateLoginUserPWD({
				data: data.field
			})
			.then(function(resp) {
				
				cmwindow.msg(resp.msg, {
					time: 5000
				});
				
				if(resp.success && resp.code === 200){
					cmwindow.close();
				}
				
			})
			.fail(function(error) {})
			.always(function() {
				elms.submitBtn.removeClass('layui-btn-disabled');
			});
			
		}else{
			elms.submitBtn.removeClass('layui-btn-disabled');
			cmwindow.msg("输入的新密码与确认密码不相同");
		}
		
		return false;
	});
});
